<script src="{{ getAssets }}js/amazeui.datatables.min.js?v={{ assets_version }}"></script>
<script>
var tables = $('#dataTables').DataTable({
	"sAjaxSource" : '{{ urlfor "ProductsController.DataGrid"}}'
	,"bLengthChange": false
	,"fnServerData" : function(sSource, aoData, fnCallback) {
		var params = checkQueryTableData(aoData);
		$('#search_key').val() ? params.filter = $('#search_key').val() : '';
		params.TypeId = defErpTypeid;
		params.Status = 1;
		params.Suppliers = Number({{.m.Suppliers.Id}});
		$.post(sSource, JSON.stringify(params),function(resp) {
			fnCallback(resp);
		},'json')
	}
	,columns: [
		{"data" : "Id", "bSortable": false}
		,{render:function(data, type, full, meta) {
			return productGroupName(full);
		 }}
		 ,{   
			"mDataProp": "Action", 
			"bSortable": false, 
			"sWidth":"10%",
			"render": function ( data, type, full, meta ) { 
				btns = '<a class="am-btn am-btn-secondary am-btn-xs open-dialog-btn" data-entryid="{{.m.Id}}" data-goodsid="'+ full.Id +'"><i class="am-icon-shopping-cart"></i></a>';
				return btns;  
			}  
		 } 
	]
	,"aLengthMenu":[20, 50, 100, 200]
	,'bLengthChange': true
	,"serverSide": true
	,"bPaginate" : true
	,"bFilter" : false	//是否启动过滤、搜索功能 
	,"bInfo" : true //是否显示页脚信息，DataTables插件左下角显示记录数
	,"bProcessing" : true //DataTables载入数据时，是否显示‘进度’提示  
	,"drawCallback": function(settings){
		
		$('.open-dialog-btn').click(function(){
			var entry_id = $(this).data('entryid');
			var goods_id = $(this).data('goodsid');
			switch(defErpTypeid) {
				case 10:
					showRangeSlider(entry_id, goods_id, 10);
					break;
				case 20:
					showRangeSlider(entry_id, goods_id, 20);
					break;	
				case 30:
					showMountingAdd(entry_id, goods_id, 0);
					break;
				case 40:
					showRangeSlider(entry_id, goods_id, 40);
					break;	
				case 41:
					showMountingAdd(entry_id, goods_id, 0);
					break;	
				case 50:
					showOtherAdd(entry_id, goods_id, 0);
					break;	
				case 60:
					showOtherAdd(entry_id, goods_id, 0);
					break;		
				case 70:
                    showOtherAdd(entry_id, goods_id, 0);
					break;		
			}
			return false;
		});
		
	}  
});

var bivariateIndex;
var rangeSliderIndex;
function showRangeSlider(entry_id, goods_id, typeid) {
	if(typeid == 40) {
		var url = '{{urlfor "ErpEntryController.PresbyopicRangeSlider"}}?entry_id='+entry_id+'&goods_id='+goods_id;
	} else {
		var url = '{{urlfor "ErpEntryController.LensRangeSlider"}}?entry_id='+entry_id+'&goods_id='+goods_id;	
	}
	rangeSliderIndex = layer.open({
		type: 2,
		title: '选择光度范围',
		shadeClose: false,
		shade: 0.2,
		maxmin: true,
		shift: 1,
		area: ['760px', '540px'],
		content: url
	});
}

function showLensBivariate(entry_id, goods_id, middle_id, ranges) {
	var url = '{{urlfor "ErpEntryController.LensBivariate"}}?entry_id='+entry_id + '&middle_id='+ middle_id +'&goods_id='+goods_id+'&ranges='+encodeURIComponent(ranges);
	bivariateIndex = layer.open({
		type: 2,
		title: '光度二维表',
		shadeClose: false,
		shade: 0.2,
		maxmin: true,
		shift: 1,
        area: ['100%', '100%'],
		content: url
	});
}
//光学镜架
function showMountingAdd(entry_id, goods_id, middle_id){
	var url = '{{urlfor "ErpEntryController.MountingAdd"}}?entry_id='+entry_id + '&middle_id='+ middle_id +'&goods_id='+goods_id;
	var title = $('.erptypeid[data-id="'+ defErpTypeid +'"]').text();
	layer.open({
		type: 2,
		title: title,
		shadeClose: false,
		shade: 0.2,
		maxmin: true,
		shift: 1,
		area: ['80%', '70%'],
		content: url
	});
}

//其它
function showOtherAdd(entry_id, goods_id, middle_id){
	var url = '{{urlfor "ErpEntryController.OtherAdd"}}?entry_id='+entry_id + '&middle_id='+ middle_id +'&goods_id='+goods_id;
	var title = $('.erptypeid[data-id="'+ defErpTypeid +'"]').text();
	layer.open({
		type: 2,
		title: title,
		shadeClose: false,
		shade: 0.2,
		maxmin: true,
		shift: 1,
		area: ['600px', '40%'],
		content: url
	});
}
//药品
function showDrugsAdd(entry_id, goods_id, middle_id){
    var url = '{{urlfor "ErpEntryController.DrugsAdd"}}?entry_id='+entry_id + '&middle_id='+ middle_id +'&goods_id='+goods_id;
    var title = $('.erptypeid[data-id="'+ defErpTypeid +'"]').text();
    layer.open({
        type: 2,
        title: title,
        shadeClose: false,
        shade: 0.2,
        maxmin: true,
        shift: 1,
        area: ['80%', '70%'],
        content: url
    });
}

function closeLensBivariate() {
	layer.close(bivariateIndex);
}
function closeSelectComplete() {
	layer.closeAll();
	$.post('{{ urlfor "ErpEntryController.GetMiddleData" }}', {'entry_id':'{{.m.Id}}'}, function(res){
		if(res.code == 0) {
			//goods-list
			$('#goods-list').html('');
			$.each(res.data, function(index, value){
				var html = '<div class="list">\
                        <p class="am-text-sm">'+ productGroupName(value.Products) +'</p>\
                        <p>X'+ value.PurchaseNum +' <a href="#" data-entry="'+ value.EntryId +'" data-goods="'+ value.Products.Id +'" data-attr="'+ encodeURIComponent(value.Attribute) +'" data-id="'+ value.Id +'" data-typeid="'+ value.Products.TypeId +'" onclick="editMiddleData(this);return false;" class="am-text-xs">编辑</a>&nbsp;&nbsp;&nbsp;<a href="#" onclick="deleteMiddleData('+ value.Id +', this);return false;" class="am-text-danger am-text-xs">删除</a></p>\
                    </div>';
					
				$('#goods-list').append(html);	
			});
		}
	}, 'json');
}
function deleteMiddleData(middle_id, h) {
	$.post('{{ urlfor "ErpEntryController.DeleteMiddleData" }}', {'middle_id':middle_id}, function(res){
		if(res.code == 0) {
			$(h).parent().parent().remove();return;
		} else {
			layer.msg(res.msg);	
		}
	}, 'json');
}
function editMiddleData(h) {
	var o = $(h);
	var typeid = o.data('typeid');
	var entry_id = o.data('entry');
	var goods_id = o.data('goods');
	var attr = o.data('attr');
	var middle_id = o.data('id');

	switch(typeid) {
		case 10:
			showLensBivariate(entry_id, goods_id, middle_id, decodeURIComponent(attr));
			break;
		case 20:
			showLensBivariate(entry_id, goods_id, middle_id, decodeURIComponent(attr));
			break;
		case 30:
			showMountingAdd(entry_id, goods_id, middle_id);
			break;
		case 40:
			showLensBivariate(entry_id, goods_id, middle_id, decodeURIComponent(attr));
			break;	
		case 41:
			showMountingAdd(entry_id, goods_id, middle_id);
			break;	
		case 50:
			showOtherAdd(entry_id, goods_id, middle_id);
			break;	
		case 60:
			showOtherAdd(entry_id, goods_id, middle_id);
			break;		
		case 70:
            showOtherAdd(entry_id, goods_id, middle_id);
			break;		
							
	}
}

closeSelectComplete();

function closeRangeSlider() {
	layer.close(rangeSlider);
}

$("#searchsubmit").bind("click", function () { //点击按钮 触发table重新请求服务器
	$("#dataTables").dataTable().fnDraw(false);
});

//类别切换事件
function defErpTypeidEvent(obj) {
    defErpTypeid= $(obj).data('id');
    $("#searchsubmit").click();
}

$('#insert-entry').click(function(){
	var url = '{{urlfor "ErpEntryController.SelectSuppliers"}}';
	
	layer.open({
		type: 2,
		title: '选对供应商',
		shadeClose: false,
		shade: 0.2,
		maxmin: true,
		shift: 1,
		area: ['410px', '468px'],
		content: url
	});
	
	return false;	
});

function SelectSuppliersRollBack(id) {
	layer.closeAll();
	location = '{{ urlfor "ErpEntryController.Edit" }}/'+id;	
}

$('#cennelBtn').click(function(){
	$.post('{{ urlfor "ErpEntryController.DeleteTemp" }}', function(res){
		if(res.code == 0) {
			location = '{{ urlfor "ErpEntryController.Index" }}';
		}
	});
});

//提交订单
$('#submitOrder').click(function(){
    var entry_id = '{{.m.Id}}';
    $.post('{{ urlfor "ErpEntryController.SubmitOrder" }}', {'entry_id':entry_id}, function(res) {
        if(res.code == 1) {
            layer.alert(res.msg);
            return false;
        }
        location = '{{ urlfor "ErpEntryController.Index" }}';
    },'json');

});

</script>